package ui;

import control.Control;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created with IntelliJ IDEA.
 * User: Deshun
 * Date: 2/14/13
 * Time: 11:39 AM
 * To change this template use File | Settings | File Templates.
 */
public class FormRowCell {

    Control control;
    private JPanel panel;
    private JLabel remove;
    private int idealHeight;
    private int idealWidth;

    //TODO remove cell and destroy control

    FormRowCell(Control control) {
        this.control = control;
        control.setCell(this);
        panel = new JPanel(new BorderLayout());
        panel.setOpaque(false);
        java.net.URL imageURL = this.getClass().getClassLoader().getResource("images/delete.png");
        ImageIcon deleteIcon = new ImageIcon(imageURL);
        remove = new JLabel();
        remove.setIcon(deleteIcon);
        remove.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseReleased(MouseEvent e) {
               removeControl();
            }
        });

        idealHeight = control.getIdealHeight();
        idealWidth = control.getIdealWidth();
    }

    Component explode() {
        if ( panel.getComponentCount() > 0 ) {
            panel.removeAll();
        }

        panel.add(control.controlPanel(), BorderLayout.CENTER);
        panel.add(remove, BorderLayout.EAST);

//        System.out.println("Cell :"+panel.getPreferredSize());
        return panel;
    }

    public void removeControl(){
        control.removeMe();
    }

    public int getIdealHeight() {
        return idealHeight;
    }

    public void setIdealHeight(int height) {
        this.idealHeight = height;
        control.setIdealHeight(height);
    }

    public int getIdealWidth() {
        return idealWidth;
    }

    public void setIdealWidth(int width) {
        this.idealWidth = width;
        control.setIdealWidth(width);
    }

    public void setPositionInRow(int positionInRow) {
        control.setPositionInRow(positionInRow);
    }

    public void setRow(int row) {
        control.setRowNumber(row);
    }

    public JPanel getPanel() {
        return panel;
    }

    public void setPanel(JPanel panel) {
        this.panel = panel;
    }

    public boolean isEmpty() {
        if ( control == null ) {
            return true;
        }
        return false;
    }
}
